using System;
using System.IO;
using System.Text;

namespace Com.FirstSolver.Splash;

public static class Base64
{
	private const string DATA_BIN2ASCII = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

	private const byte B64_NOT_BASE64 = 243;

	private static readonly byte[] DATA_ASCII2BIN = new byte[128]
	{
		255, 255, 255, 255, 255, 255, 255, 255, 255, 224,
		240, 255, 255, 241, 255, 255, 255, 255, 255, 255,
		255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
		255, 255, 224, 255, 255, 255, 255, 255, 255, 255,
		255, 255, 255, 62, 255, 242, 255, 63, 52, 53,
		54, 55, 56, 57, 58, 59, 60, 61, 255, 255,
		255, 0, 255, 255, 255, 0, 1, 2, 3, 4,
		5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
		15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
		25, 255, 255, 255, 255, 255, 255, 26, 27, 28,
		29, 30, 31, 32, 33, 34, 35, 36, 37, 38,
		39, 40, 41, 42, 43, 44, 45, 46, 47, 48,
		49, 50, 51, 255, 255, 255, 255, 255
	};

	public static string Encode(byte[] inArray)
	{
		if (inArray == null)
		{
			return null;
		}
		int Length = (inArray.Length + 2) / 3 << 2;
		StringBuilder sb = new StringBuilder(Length);
		int Index = 0;
		for (int i = inArray.Length; i > 0; i -= 3)
		{
			if (i >= 3)
			{
				byte b0 = inArray[Index++];
				byte b2 = inArray[Index++];
				byte b4 = inArray[Index++];
				sb.Append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[b0 >> 2]);
				sb.Append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[((b0 << 4) | (b2 >> 4)) & 0x3F]);
				sb.Append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[((b2 << 2) | (b4 >> 6)) & 0x3F]);
				sb.Append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[b4 & 0x3F]);
			}
			else
			{
				byte b = inArray[Index++];
				byte b3 = (byte)((i == 2) ? inArray[Index++] : 0);
				sb.Append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[b >> 2]);
				sb.Append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[((b << 4) | (b3 >> 4)) & 0x3F]);
				if (i == 1)
				{
					sb.Append('=');
				}
				else
				{
					sb.Append("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"[(b3 << 2) & 0x3F]);
				}
				sb.Append('=');
			}
		}
		return sb.ToString();
	}

	public static byte[] Decode(string base64String)
	{
		if (string.IsNullOrEmpty(base64String))
		{
			return null;
		}
		int Length = base64String.Length;
		using MemoryStream memStream = new MemoryStream((Length + 3 >> 2) * 3);
		int Index = 0;
		byte Previous = 0;
		for (int i = 0; i < Length; i++)
		{
			int CharValue = base64String[i];
			if (CharValue >= 128)
			{
				throw new ArgumentException();
			}
			byte OrderValue = DATA_ASCII2BIN[CharValue];
			if (OrderValue == byte.MaxValue)
			{
				throw new ArgumentException();
			}
			if ((OrderValue | 0x13) == 243)
			{
				continue;
			}
			switch (Index)
			{
			case 0:
				if (CharValue == 61)
				{
					throw new ArgumentException();
				}
				Previous = OrderValue;
				Index++;
				continue;
			case 1:
				if (CharValue == 61)
				{
					throw new ArgumentException();
				}
				memStream.WriteByte((byte)((Previous << 2) | (OrderValue >> 4)));
				Previous = OrderValue;
				Index++;
				continue;
			case 2:
				if (CharValue == 61)
				{
					Index = 0;
					break;
				}
				memStream.WriteByte((byte)((Previous << 4) | (OrderValue >> 2)));
				Previous = OrderValue;
				Index++;
				continue;
			default:
				Index = 0;
				if (CharValue == 61)
				{
					break;
				}
				memStream.WriteByte((byte)((Previous << 6) | OrderValue));
				continue;
			}
			break;
		}
		if (Index == 0)
		{
			return memStream.ToArray();
		}
		throw new ArgumentException();
	}
}
